package cn.daniellee.plugin.ce.command;

import cn.daniellee.plugin.ce.CraftEconomy;
import cn.daniellee.plugin.ce.core.RankingCore;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.math.BigDecimal;

public class EconomyCommand implements CommandExecutor {

	@Override
	public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
		if ("balance".equalsIgnoreCase(command.getName())) {
			if (strings.length > 0) {
				OfflinePlayer player = Bukkit.getOfflinePlayer(strings[0]);
				if (player.getName() != null) {
					double balance = CraftEconomy.getInstance().getEconomy().getBalance(player);
					commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.balance-other", "&ePlayer &b{player} &ebalance: &6{balance}").replace("{player}", player.getName()).replace("{balance}", CraftEconomy.getInstance().getFormat().format(balance))).replace("&", "§"));
				} else commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.invalid-player", "&eTarget player does not exist.")).replace("&", "§"));
			} else if (commandSender instanceof Player) {
				double balance = CraftEconomy.getInstance().getEconomy().getBalance((OfflinePlayer) commandSender);
				commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.balance-self", "&eYour balance: &6{balance}").replace("{balance}", CraftEconomy.getInstance().getFormat().format(balance))).replace("&", "§"));
			} else commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.console-cant", "&eThe console cannot execute this command.")).replace("&", "§"));
		} else if ("pay".equalsIgnoreCase(command.getName()) && strings.length > 1) {
			if (commandSender instanceof Player) {
				OfflinePlayer player = Bukkit.getOfflinePlayer(strings[0]);
				if (player.getName() != null) {
					double amount = Double.parseDouble(strings[1]);
					if (amount > 0) {
						if (CraftEconomy.getInstance().getEconomy().withdrawPlayer((OfflinePlayer) commandSender, amount).type == EconomyResponse.ResponseType.SUCCESS) {
							CraftEconomy.getInstance().getEconomy().depositPlayer(player, amount);
							if (player.isOnline() && player.getPlayer() != null) {
								player.getPlayer().sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.payment-received", "&eYou have received &6{money} from &b{player}").replace("{money}", CraftEconomy.getInstance().getFormat().format(amount)).replace("{player}", commandSender.getName())).replace("&", "§"));
							}
							commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.payment-sent", "&eYou paid &6{money} to &b{player}").replace("{money}", CraftEconomy.getInstance().getFormat().format(amount)).replace("{player}", player.getName())).replace("&", "§"));
						} else commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.you-nomoney", "&eYour balance is insufficient, payment failed.")).replace("&", "§"));
					} else commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.invalid-amount", "&eInvalid amount, please re-enter.")).replace("&", "§"));
				} else commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.invalid-player", "&eTarget player does not exist.")).replace("&", "§"));
			} else commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.console-cant", "&eThe console cannot execute this command.")).replace("&", "§"));
		} else if ("balancetop".equalsIgnoreCase(command.getName())) {
			int page = strings.length > 0 ? Integer.parseInt(strings[0]) : 1;
			RankingCore.sendRanking(commandSender, page > 1 ? page : 1);
		} else if ("money".equalsIgnoreCase(command.getName()) && commandSender.isOp()) {
			if (strings.length > 0 && "reload".equalsIgnoreCase(strings[0])) {
				CraftEconomy.getInstance().reloadConfig();
				if (CraftEconomy.getInstance().loadConfig()) {
					commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.reload-success", "&eReload has been successful.")).replace("&", "§"));
				}
			} else if (strings.length > 2) {
				OfflinePlayer player = Bukkit.getOfflinePlayer(strings[1]);
				if (player.getName() != null) {
					double amount = Double.parseDouble(strings[2]);
					if (amount > 0) {
						if ("take".equalsIgnoreCase(strings[0])) {
							if (CraftEconomy.getInstance().getEconomy().withdrawPlayer(player, amount).type == EconomyResponse.ResponseType.SUCCESS) {
								commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.take-success", "&eSuccessfully removed &6{money} from &b{player}").replace("{money}", CraftEconomy.getInstance().getFormat().format(amount)).replace("{player}", player.getName())).replace("&", "§"));
							} else commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.target-nomoney", "&eInsufficient target player balance.")).replace("&", "§"));
						} else if ("give".equalsIgnoreCase(strings[0])) {
							CraftEconomy.getInstance().getEconomy().depositPlayer(player, amount);
							commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.give-success", "&eSuccessfully gave &6{money} &eto &b{player}").replace("{money}", CraftEconomy.getInstance().getFormat().format(amount)).replace("{player}", player.getName())).replace("&", "§"));
						} else if ("set".equalsIgnoreCase(strings[0])) {
							double balance = CraftEconomy.getInstance().getEconomy().getBalance(player);
							if (balance != amount) {
								if (balance > amount) {
									CraftEconomy.getInstance().getEconomy().withdrawPlayer(player, new BigDecimal(balance).subtract(new BigDecimal(amount)).doubleValue());
								} else {
									CraftEconomy.getInstance().getEconomy().depositPlayer(player, new BigDecimal(amount).subtract(new BigDecimal(balance)).doubleValue());
								}
								commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.set-success", "&eSuccessfully set &6{money} &eto &b{player}").replace("{money}", CraftEconomy.getInstance().getFormat().format(amount)).replace("{player}", player.getName())).replace("&", "§"));
							} else commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.invalid-amount", "&eInvalid amount, please re-enter.")).replace("&", "§"));
						} else sendHelp(commandSender);
					} else commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.invalid-amount", "&eInvalid amount, please re-enter.")).replace("&", "§"));
				} else commandSender.sendMessage((CraftEconomy.getInstance().getPrefix() + CraftEconomy.getInstance().getConfig().getString("message.invalid-player", "&eTarget player does not exist.")).replace("&", "§"));
			} else sendHelp(commandSender);
		} else sendHelp(commandSender);
		return true;
	}

	private void sendHelp(CommandSender commandSender) {
		commandSender.sendMessage(("&m&6---&m&a--------&3 : " + CraftEconomy.getInstance().getConfig().getString("prompt-prefix", "&6CraftEconomy") + "&3 : &m&a--------&m&6---").replace("&", "§"));

		String balanceText = CraftEconomy.getInstance().getConfig().getString("help.balance", "&eView your own balance.").replace("&", "§");
		TextComponent balanceHelp = new TextComponent("/balance  " + balanceText);
		balanceHelp.setColor(ChatColor.GRAY);
		balanceHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/balance"));
		balanceHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(balanceText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(balanceHelp);

		String balanceOtherText = CraftEconomy.getInstance().getConfig().getString("help.balance-other", "&eView other player's balances.").replace("&", "§");
		TextComponent balanceOtherHelp = new TextComponent("/balance <player>  " + balanceOtherText);
		balanceOtherHelp.setColor(ChatColor.GRAY);
		balanceOtherHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/balance "));
		balanceOtherHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(balanceOtherText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(balanceOtherHelp);

		String payText = CraftEconomy.getInstance().getConfig().getString("help.pay", "&ePay your balance to other players.").replace("&", "§");
		TextComponent payHelp = new TextComponent("/pay <player> <amount>  " + payText);
		payHelp.setColor(ChatColor.GRAY);
		payHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pay "));
		payHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(payText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(payHelp);

		String balancetopText = CraftEconomy.getInstance().getConfig().getString("help.balancetop", "&eView player balance leaderboards.").replace("&", "§");
		TextComponent balancetopHelp = new TextComponent("/balancetop  " + balancetopText);
		balancetopHelp.setColor(ChatColor.GRAY);
		balancetopHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/balancetop"));
		balancetopHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(balancetopText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(balancetopHelp);

		if (commandSender.isOp()) {
			String moneyTakeText = CraftEconomy.getInstance().getConfig().getString("help.money-take", "&eReduce the balance of a player.").replace("&", "§");
			TextComponent moneyTakeHelp = new TextComponent("/money take <player> <amount>  " + moneyTakeText);
			moneyTakeHelp.setColor(ChatColor.GRAY);
			moneyTakeHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/money take "));
			moneyTakeHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(moneyTakeText).color(ChatColor.BLUE).create()));
			commandSender.spigot().sendMessage(moneyTakeHelp);

			String moneyGiveText = CraftEconomy.getInstance().getConfig().getString("help.money-give", "&eIncrease the balance of a player.").replace("&", "§");
			TextComponent moneyGiveHelp = new TextComponent("/money give <player> <amount>  " + moneyGiveText);
			moneyGiveHelp.setColor(ChatColor.GRAY);
			moneyGiveHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/money give "));
			moneyGiveHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(moneyGiveText).color(ChatColor.BLUE).create()));
			commandSender.spigot().sendMessage(moneyGiveHelp);

			String moneySetText = CraftEconomy.getInstance().getConfig().getString("help.money-set", "&eSet a player's balance.").replace("&", "§");
			TextComponent moneySetHelp = new TextComponent("/money set <player> <amount>  " + moneySetText);
			moneySetHelp.setColor(ChatColor.GRAY);
			moneySetHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/money set "));
			moneySetHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(moneySetText).color(ChatColor.BLUE).create()));
			commandSender.spigot().sendMessage(moneySetHelp);

			String reloadText = CraftEconomy.getInstance().getConfig().getString("help.reload", "&eReload configuration.").replace("&", "§");
			TextComponent reloadHelp = new TextComponent("/money reload  " + reloadText);
			reloadHelp.setColor(ChatColor.GRAY);
			reloadHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/money reload"));
			reloadHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(reloadText).color(ChatColor.BLUE).create()));
			commandSender.spigot().sendMessage(reloadHelp);
		}
	}

}
